package datos;

import java.io.*;
import java.util.ArrayList;

public class GuardarDatos<T> {
    ObjectOutputStream oos;
    ObjectInputStream ois;
       
    public void Guardar(ArrayList<T> obj,String fichero) throws FileNotFoundException, IOException{
        try{
            oos = new ObjectOutputStream(new FileOutputStream(fichero));
            for (T t:obj)
                oos.writeObject(t);
            oos.close();
        }catch(Exception e){
            System.out.println(e);
        }
    }
    
    public ArrayList<T> Cargar(String fichero) throws IOException{
        ArrayList<T> lista = new ArrayList<>();
        try{   
            ois = new ObjectInputStream(new FileInputStream(fichero));
            Object aux = ois.readObject();
            while (aux!=null){
                    lista.add((T) aux);   
                    aux = ois.readObject();
            }
            ois.close();
        }catch(IOException | ClassNotFoundException e){
            System.out.println(e);
        }  
        return lista;
    }
    
    public void GuardarTextoPlano(ArrayList<T> salida, String txt){
        try{
            FileWriter fichero = new FileWriter(txt);
            for(T s:salida){
                fichero.write(s + "\n");
            }
             fichero.close();
        }catch(Exception e){ System.out.println(e); } 
    }
    
}
